/**
 * Copyright (c) Jan 15, 2013 8:26:32 PM WKM and/or its affiliates. 
 * Use is subject to license terms.
 * All rights reserved.
 */
package org.extblog.wkm.web.service.face.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.extblog.wkm.web.dao.face.BodyDao;
import org.extblog.wkm.web.dao.face.FaceDao;
import org.extblog.wkm.web.dao.face.HomePageDao;
import org.extblog.wkm.web.dao.music.MusicDao;
import org.extblog.wkm.web.service.AbstractService;
import org.extblog.wkm.web.service.face.FaceService;
import org.extblog.wkm.web.util.DateFormatPattern;
import org.extblog.wkm.web.vo.BodyItemFaceView;
import org.extblog.wkm.web.vo.HomePageFaceView;
import org.extblog.wkm.web.vo.LeftItemFaceView;
import org.extblog.wkm.web.vo.MusicFormView;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Component;

/**
 * @author WKM
 * FaceServiceImpl.java history
 * 1.WKM - Jan 15, 2013 - Create File
 */
@Component
public class FaceServiceImpl extends AbstractService implements FaceService {
	
	/** dao **/
	private FaceDao faceDao = null;
	
	private HomePageDao homePageDao = null;
	
	private BodyDao bodyDao = null;
	
	private MusicDao musicDao = null;
	
	public FaceDao getFaceDao() {
		return faceDao;
	}
	@Resource
	public void setFaceDao(FaceDao faceDao) {
		this.faceDao = faceDao;
	}
	
	public BodyDao getBodyDao() {
		return bodyDao;
	}
	@Resource
	public void setBodyDao(BodyDao bodyDao) {
		this.bodyDao = bodyDao;
	}
	public HomePageDao getHomePageDao() {
		return homePageDao;
	}
	@Resource
	public void setHomePageDao(HomePageDao homePageDao) {
		this.homePageDao = homePageDao;
	}
	public MusicDao getMusicDao() {
		return musicDao;
	}
	@Resource
	public void setMusicDao(MusicDao musicDao) {
		this.musicDao = musicDao;
	}
	/* (non-Javadoc)
	 * @see org.extblog.wkm.web.service.face.FaceService#getBodyItems(java.lang.String)
	 */
	@Override
	public List<String> getBodyItems(String bodyId) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see org.extblog.wkm.web.service.face.FaceService#getLeftItems()
	 */
	@Override
	public List<LeftItemFaceView> getLeftItems() {
		// TODO Auto-generated method stub
		List<LeftItemFaceView> result = new ArrayList<LeftItemFaceView>();
		List<Map<String, String>> roots = faceDao.getRootItems();
		if(roots == null){
			log.info("没有查询到根节点数据roots=null");
			return result;
		}
		for(Map<String, String> root:roots){
			LeftItemFaceView view = new LeftItemFaceView();
			view.setItemId(root.get("itemId"));
			view.setItemName(root.get("itemName"));
			List<LeftItemFaceView> list = new ArrayList<LeftItemFaceView>();
			List<Map<String, String>> children = faceDao.getItemsByParentId(view.getItemId());
			if(children != null){
				for(Map<String, String> child:children){
					LeftItemFaceView childView = new LeftItemFaceView();
					childView.setItemId(child.get("itemId"));
					childView.setItemName(child.get("itemName"));
					list.add(childView);
				}
				log.info( root.get("itemName") + "-的子节点:" + ArrayUtils.toString(list));
			}
			
			view.setChildren(list);
			result.add(view);
		}
		log.info("返回数据条数:" + result.size());
		log.info("返回数据:" + ArrayUtils.toString(result));
		return result;
	}
	

	/**
	 * 增加根节点
	 */
	public boolean addRootItem(LeftItemFaceView view) {
		// TODO Auto-generated method stub
		view.setItemId(DateFormatPattern.ID + DateFormatPattern.SPLIT_CHAR + DateFormatUtils.format(Calendar.getInstance(),DateFormatPattern.DATE_TIME));
		Map<String, Object> map = converMap(view);
		log.info("增加数据=" + map);
		int row = faceDao.insertItem(map);
		log.info("影响数据库数据条数:" + row);
		return row >= 0;
	}
	
	private Map<String, Object> converMap(LeftItemFaceView view){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("itemParent",view.getItemParent());
		map.put("itemName",view.getItemName());
		map.put("itemType",view.getItemType());
		map.put("itemSeq",view.getItemSeq());
		map.put("itemDesc",view.getItemDesc());
		map.put("itemId",view.getItemId());
		return map;
	}

	public HomePageFaceView getHomePage() {
		// TODO Auto-generated method stub
		List<Map<String,String>> table = homePageDao.selectHomePage();
		if(table == null || table.size() == 0){
			log.info("没有查询到记录...");
			return new HomePageFaceView();
		}
		log.info("查询到数据条数:" + table.size());
		Map<String,String> map = table.get(Calendar.getInstance().get(Calendar.DAY_OF_YEAR) % table.size());
		BeanMap beanMap = BeanMap.create(new HomePageFaceView());
		beanMap.putAll(map);
		log.info("通过计算得知首页应显示记录为:" + beanMap.getBean());
		return (HomePageFaceView)beanMap.getBean();
	}

	public boolean addBodys(BodyItemFaceView view) {
		// TODO Auto-generated method stub
		view.setItemId(DateFormatPattern.ID + DateFormatPattern.SPLIT_CHAR + DateFormatUtils.format(Calendar.getInstance(),DateFormatPattern.DATE_TIME));
		log.info("增加数据=" + view);
		int row = bodyDao.insertBody(view);
		log.info("影响数据库数据条数:" + row);
		return row >= 0;
	}

	public boolean addHomePage(HomePageFaceView view) {
		// TODO Auto-generated method stub
		view.setUrlId(DateFormatPattern.ID + DateFormatPattern.SPLIT_CHAR + DateFormatUtils.format(Calendar.getInstance(),DateFormatPattern.DATE_TIME));
		log.info("增加数据=" + view);
		int row = homePageDao.insertHomePage(view);
		log.info("影响数据库数据条数:" + row);
		return row >= 0;
	}
	
	public List<Map<String,String>> loadBodys(LeftItemFaceView view) {
		// TODO Auto-generated method stub
		log.info("获取所有连接父节点是:" + view.getItemId());
		BodyItemFaceView v = new BodyItemFaceView();
		v.setItemParent(view.getItemId());
		List<Map<String,String>> list = bodyDao.selectBodys(v);
		if(list == null){
			list =new  ArrayList<Map<String,String>>();
		}
		log.info("获取数据条数是:" + list.size());
		return list;
	}
	@Override
	public LeftItemFaceView getLeftItems(String itemId) {
		// TODO Auto-generated method stub
		List<Map<String,String>> result = faceDao.getRootItems(itemId);
		if(result == null){
			result = new ArrayList<Map<String,String>>();
		}
		BeanMap beanMap = BeanMap.create(new LeftItemFaceView());
		if(result.size() > 0){
			beanMap.putAll(result.get(0));
		}
		log.info("获取数据条数是:" + result.size());
		if(result.size() > 0){
			return (LeftItemFaceView)beanMap.getBean();
		}
		return new LeftItemFaceView();
	}
	
	@Override
	public List<MusicFormView> getMusics() {
		// TODO Auto-generated method stub
		List<MusicFormView> result = new ArrayList<MusicFormView>();
		List<Map<String,String>> list = musicDao.selectMusics();
		if(list == null){
			return result;
		}
		log.info("查询到音乐记录为:" + list.size());
		try{
			for(Map<String,String> map:list){
				MusicFormView temp = new MusicFormView();
				BeanUtils.populate(temp, map);
				result.add(temp);
			}
		} catch (IllegalAccessException e){
			e.printStackTrace();
			log.error("错误信息:" + e.getMessage());
		} catch (InvocationTargetException e){
			e.printStackTrace();
			log.error("错误信息:" + e.getMessage());
		}
		return result;
	}
}
